package cn.itcast.wushangxiantu.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.YouXiangAdapter;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.UserData.UserYouXiang;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class YouXiang extends AppCompatActivity {

    private ListView lv;
    private UserYouXiang youXiang;
    private YouXiangAdapter youXiangAdapter;
    int num;
    private TextView tvAll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_xiang);
        initView();
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                num = i;
                String content = youXiang.getYouXiang().get(i).getContent() + "\n\n时间:" + youXiang.getYouXiang().get(i).getTime();
                String hand = "来自：" + youXiang.getYouXiang().get(i).getHand();
                GeneralDialog(hand, content, youXiang.getYouXiang().get(i).getId());
            }
        });
        getYouXiang();
        tvAll = (TextView) findViewById(R.id.tvAll);
        tvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getAllJiangli();
            }
        });
    }

    private void getYouXiang() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=youxiangyulan", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("youxiangyulan1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("youxiangyulan2", src);
                UserYouXiang user = null;
                try {
                    user = new Gson().fromJson(src, UserYouXiang.class);
                }catch (Exception e){
                    e.getMessage();
                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            switch (message.what){
                case 1:
                    youXiang= (UserYouXiang) message.obj;
                    if (null!=youXiang){
                        if (youXiangAdapter == null) {
                            youXiangAdapter = new YouXiangAdapter(YouXiang.this);
                        }
                        youXiangAdapter.setUserYouXiang(youXiang);
                        lv.setAdapter(youXiangAdapter);
                    }else{
                        ToastUtil.toast(YouXiang.this,"服务器炸了");
                    }
                    break;
                case 2:
                    User user = (User) message.obj;
                    if (null!=user){
                        if (user.getStw().equals("OK")) {
                            ToastUtil.toast(YouXiang.this, "领取成功");
                            Login.getUser().setMoney(user.getMoney());
                            youXiang.getYouXiang().remove(num);
                            youXiangAdapter.setUserYouXiang(youXiang);
                            EventBus.getDefault().post(new MessageEvent("setmoney"));
                            EventBus.getDefault().post(new MessageEvent("YouXiang"));
                            getYouXiang();
                        } else {
                            ToastUtil.toast(YouXiang.this, user.getStw());
                        }
                    }else{
                        ToastUtil.toast(YouXiang.this,"服务器炸了");
                    }
                    break;
                case 3:
                     user = (User) message.obj;
                    if (null!=user){
                        if (user.getStw().equals("OK")) {
                            ToastUtil.toast(YouXiang.this, "领取成功");
                            Login.getUser().setMoney(Login.getUser().getMoney()+user.getMoney());
                            youXiang.getYouXiang().clear();
                            youXiangAdapter.setUserYouXiang(youXiang);
                            EventBus.getDefault().post(new MessageEvent("setmoney"));
                            EventBus.getDefault().post(new MessageEvent("YouXiang"));
                            getYouXiang();
                        } else {
                            ToastUtil.toast(YouXiang.this, user.getStw());
                        }
                    }else{
                        ToastUtil.toast(YouXiang.this,"服务器炸了");
                    }
                    break;
            }
            return false;
        }
    });

    public void GeneralDialog(String head, String content, final int id) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(YouXiang.this)
                .setTitle(head)

                .setMessage(content)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        getJiangli(id);
                        dialogInterface.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void getJiangli(int id) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=getJiangli" + "&id=" + id, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("getjiangli1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("getjiangli2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                }catch (Exception e){
                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }
    private void getAllJiangli() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + Login.getUser().getUsernumber() + "&need=getAllJiangli" , new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("getjiangli1", e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("getjiangli2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                }catch (Exception e){

                }
                Message message = new Message();
                message.obj = user;
                message.what = 3;
                handler.sendMessage(message);
            }
        });
    }
}
